#!/bin/sh

e(){
	echo `basename $0`: "$@"
}
e "This script will download everything you need to compile"
e " programs for PPC (GameCube, Wii)":
e
e "The files will be downloaded under 'devkitpro' directory."
e " created here: "`pwd -P`"/devkitpro"
e
e "file structure will be:"
e `pwd -P`"/devkitpro"
e "           |"
e "           +-- devkitPPC"
e "           +-- examples"
e "           |     |"
e "           |     +-- gamecube"
e "           |     +-- wii"
e "           +-- gcube"
e "           +-- libogc"
e "                +- SDL
e
e "hit Ctrl+C to cancel."
e "hit RETURN to continue"
read dummyPauseMe

e "devkitPRO"
mkdir devkitpro 2>/dev/null
cd devkitpro
	e " |-- downloading";
	wget -qc http://downloads.sourceforge.net/devkitpro/devkitPPC_r16-1-`uname -m`-linux.tar.bz2
	e " |-- extracting";
	tar xjf devkitPPC*.tar.bz2
	#rm devkitPPC*.tar.bz2
	e " '-- done";

	e "libogc"
	mkdir libogc 2>/dev/null
		cd libogc
		e " |-- downloading";
		wget -qc http://downloads.sourceforge.net/devkitpro/libogc-1.7.0.tar.bz2
		e " |-- extracting";
		tar xjf libogc*.tar.bz2
		#rm libogc*.tar.bz2
		e " '-- done";

			e "SDL"
			mkdir include/SDL 2>/dev/null
			cd include/SDL
				e " |-- downloading";
				wget -qc 'http://mindcry.cpl.de/wii/bin_SDL-Port.7z'
				e " |-- extracting";
				if [ ! -x /usr/bin/7zr ]; then
					#TODO: check for 7zip full
					# if [ ! -x /usr/bin/7z ]; then
					e " WARNING"
					e " SDL is packed with 7zip"
					e " press any key to run 'sudo apt-get install p7zip' ctrl+C to cancel"
					read dummyPauseMe
					sudo apt-get install p7zip
				fi
				7zr -y x bin_SDL-Port.7z > /dev/null
				# move header files to $DEVKITPRO/libogc/include/SDL/ 
				find SDL* -name \*.h -exec mv \{\} . \;
				e " '-- done";
			cd ../..

	#exit libogc
	cd ..

	mkdir examples 2>/dev/null
	cd examples

		e "Examples WII"
		mkdir wii 2>/dev/null
		cd wii
			e " |-- downloading";
			wget -qc http://downloads.sourceforge.net/devkitpro/wii-examples-20081206.tar.bz2
			e " |-- extracting";
			tar xjf wii-examples*.tar.bz2
			#rm wii-examples*.tar.bz2
			e " '-- done";
		cd ..

		e "Examples GameCube"
		mkdir gamecube 2>/dev/null
		cd gamecube
			e " |-- downloading";
			wget -qc http://downloads.sourceforge.net/devkitpro/gamecube-examples-20081206.tar.bz2
			e " |-- extracting";
			tar xjf gamecube-examples*.tar.bz2
			#rm gamecube-examples*.tar.bz2
			e " '-- done";
		cd ..

		e "Example MakeFile"
		e " |-- downloading";
		wget -q http://assortedwiicode.googlecode.com/svn/trunk/automatii/Makefile
		e " '-- done";

	cd ..

	e "gcube Emulator"
	#mkdir gcube 2>/dev/null
	#cd gcube
		e " |-- downloading";
		wget -qc http://downloads.sourceforge.net/devkitpro/gcube-0.4.0-src.tar.bz2
		e " |-- extracting";
		tar xjf gcube*.tar.bz2
		#rm gcube*.tar.bz2
		e " '-- done";
	#cd ..

	e "Environment script"
	e " |-- creating devkitpro_env.sh"
	echo "#!/bin/sh" > devkitpro_env.sh
	echo "export DEVKITPRO="`pwd -P` >> devkitpro_env.sh
	echo "export DEVKITPPC=\$DEVKITPRO/devkitPPC" >> devkitpro_env.sh
	echo "PATH=\$PATH:\$DEVKITPPC/bin:\$DEVKITPRO/wiiload/lin32" >> devkitpro_env.sh
	chmod +x devkitpro_env.sh
	e " |-- linking env script to your .bash_profile, user "`whoami`
	if [ "0" -eq `grep -c devkitpro_env.sh ~/.bash_profile` ]; then
		echo "\n" >> ~/.bash_profile
		echo `pwd -P`"/devkitpro_env.sh" >> ~/.bash_profile
		e " '-- done";
	else
		e "ERROR! you already have the env script in your .bash_profile"
		e "       fix it manually."
		e "       it should point to: "`pwd -P`"/devkitpro_env.sh"
	fi

cd ..

#tree -d devkitpro

e
e
e "The sample MakeFile works with an option WII=1 to compile"
e "with wii rules. without that option, it uses the gamecube"
e "rules."
e "Using 'make WII=1 run' will send the code to wiiload."
e "while 'make run' will send the file to gcube emulator"

#if [ -f /etc/lsb-release ]; then
	#grep Ubuntu /etc/lsb-release
#overkill...

e
e "now, would you like to download SDL lib, zlib, jpeglib, a"
e "C compiler and let me build the gcube emulator?"
e "I will ask for sudo password to run apt-get"
e
e "hit Ctrl+C to cancel."
e "hit RETURN to continue"
read dummyPauseMe

cd devkitpro/gcube
	e
	e
	e "will run: sudo apt-get install libsdl1.2-dev zlib1g-dev libjpeg62-dev"
	e "hit Return to go on"
	read dummyPauseMe
	sudo apt-get install libsdl1.2-dev zlib1g-dev libjpeg62-dev

	e
	e "Compiling gcube"
	make

	e
	e "moving the binary to a place on the path"
	e "sudo mv gcube /usr/local/bin/gcube"
	sudo mv gcube /usr/local/bin/gcube
cd ../..

e
e
e "Everything downloaded and installed."
e "Go to http://wiibrew.org/wiki/DevkitPro"
exit 0
